package com.mall.shopping.services.carts;

import com.mall.shopping.IDeleteGoodsInCartsService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.DeleteGoodsInCartsResponse;
import org.apache.dubbo.config.annotation.Service;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author TL
 * @created Date:2020/12/20 Time:21:37
 **/

@Component
@Service(interfaceClass = IDeleteGoodsInCartsService.class)
public class IDeleteGoodsInCartsServiceImpl implements IDeleteGoodsInCartsService{

    @Autowired
    RedissonClient redissonClient;

    @Override
    public DeleteGoodsInCartsResponse deleteGoodsInCarts(Integer uid, Integer goodsId,Integer id) {
        DeleteGoodsInCartsResponse deleteGoodsInCartsResponse = new DeleteGoodsInCartsResponse();

        if (!uid.equals(id)){
            deleteGoodsInCartsResponse.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            deleteGoodsInCartsResponse.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
            return deleteGoodsInCartsResponse;
        }

        RMap<Long, Object> map = redissonClient.getMap(uid.toString());
        long l = map.fastRemove(goodsId.longValue());

        // TODO: 为什么l = 0？
        /*if (l != 1){
            deleteGoodsInCartsResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            deleteGoodsInCartsResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            return deleteGoodsInCartsResponse;
        }*/
        deleteGoodsInCartsResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        deleteGoodsInCartsResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return deleteGoodsInCartsResponse;
    }




}
